package chapter10_Advanced._thread;

/**
 * ClassName: WindowTest
 * Package: chapter10_Advanced._thread
 * Description:
 *
 * 窗口买票--多线程--使用同步方法解决线程安全问题
 *
 * @Author SherryAn
 * @Create 2025/7/31 13:38
 */

class SaleTicket1 implements Runnable {

    int ticket = 100;
    boolean isFlag = true;

    @Override
    public void run() {
        while (isFlag) {
//            synchronized (this){
                show();
//            }
        }
    }

    public synchronized void show(){//此时的同步监视器是this。此题目中为s,是唯一的
        if (ticket>0){
            try {
                Thread.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"售票，票号为："+ticket);
            ticket--;
        }else {
            isFlag=false;
        }
    }
}

public class WindowTest {

    public static void main(String[] args) {

        SaleTicket1 s=new SaleTicket1();

        Thread t1=new Thread(s);
        Thread t2=new Thread(s);
        Thread t3=new Thread(s);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}
